package tutorial;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import chanfoofoo.* ; 
import com.opensymphony.xwork2.ActionSupport;

public class UserLogin extends ActionSupport implements SessionAware {
    private String user_id ; 
    private String user_pwd ; 
    private boolean user_create ; 
    private String disp_mess ; 
    
    private Map session ; 
    public String execute() throws Exception {
    	StringBuffer mess = new StringBuffer("") ; 
    	mess.append("Running : UserLogin.execute()..." + 
        		user_id + " " + user_pwd) ; 
    	if (user_create) {
    		mess.append(" , Create user_create = true"); 
    	}
    	
    	if ((user_id != null) && (user_pwd != null)) { 
    		User_manager user_manager = new User_manager() ;
    		User currentUser = null ; 
    		if (! user_create) {  
    			currentUser = user_manager.getUser(user_id, user_pwd) ;
    		} else {
    			currentUser = user_manager.addUser(user_id, user_pwd) ;
    			if (currentUser == null) {
    				mess.append("User Already Exist!!") ; 
    			}
    		}
    		
        	disp_mess = mess.toString() ; 
        	System.out.println(mess) ;
    		if (currentUser != null) {
    			session.put("currentUser" , currentUser) ; 
    			//session.put("usernamexxx" , user_id) ;
    			return "redirect" ;   
    		} else {
    			// user_manager.addUser(user_id, user_pwd) ;
    		}
    	}	
    	return INPUT;
    }
	public String getUser_id() {
		return user_id;
	}
	public void setUser_id(String userId) {
		user_id = userId;
	}
	public String getUser_pwd() {
		return user_pwd;
	}
	public void setUser_pwd(String userPwd) {
		user_pwd = userPwd;
	}
	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		session = arg0 ; 
	}
	public void setUser_create(boolean user_create) {
		this.user_create = user_create;
	}
	public boolean isUser_create() {
		return user_create;
	}
	public void setDisp_mess(String disp_mess) {
		this.disp_mess = disp_mess;
	}
	public String getDisp_mess() {
		return disp_mess;
	}
}
